#include<stdio.h>
#include<string.h>

int romanToInt(char * s)
{
    int sum=0,len = strlen(s); //define sum and calc length of string
    for(int i=0;i<len;i++) //read each char
    {
        if(*(s+i)=='I')
        {
            if((*(s+i+1)=='V' || *(s+i+1)=='X')) //possible subtraction cases
            {
                i++;                 // reading next char and avoiding re-reading it
                if(*(s+i)=='V') sum += 4; //5 becomes 4 because of subtraction
                else if(*(s+i)=='X') sum += 9; //10 becomes 9 because of subtraction
            }
            else sum += 1;
        }
        else if(*(s+i)=='V') sum += 5;
        else if(*(s+i)=='X')
        {
            if((*(s+i+1)=='L' || *(s+i+1)=='C'))
            {
                i++;
                if(*(s+i)=='L') sum += 40;
                else if(*(s+i)=='C') sum += 90;
            }
            else sum += 10;
        }
        else if(*(s+i)=='L') sum += 50;
        else if(*(s+i)=='C')
        {
            if((*(s+i+1)=='D' || *(s+i+1)=='M'))
            {
                i++;
                if(*(s+i)=='D') sum += 400;
                else if(*(s+i)=='M') sum += 900;
            }
            else sum += 100;
        }
        else if(*(s+i)=='D') sum += 500;
        else if(*(s+i)=='M') sum += 1000;
    }
    return sum; //final integer    
} 